package com.hfb.mashibing.alip8.threadpool;

import com.hfb.mashibing.alip8.util.SleepHelper;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class MyThreadPoolExecutor {
    private volatile static int endNum = -1;   // 不结束的任务

    public static void main(String[] args) throws IOException {
        ThreadPoolExecutor tp = new ThreadPoolExecutor(
            2,      // 核心线程数：线程池维系的最小线程数
            4,   // 最大线程数：峰值时运行的最大的线程数
            60,
            TimeUnit.SECONDS,   // 60秒 最大的线程数，空闲60秒 后缩小到核心线程数
            new LinkedBlockingQueue<>(4),   // 阻塞队列线程安全，
            Executors.defaultThreadFactory(),       // 建议自定义——及实现 ThreadFactory 接口

            // 线程池对拒绝任务的处理策略
            // ThreadPoolExecutor.AbortPolicy:丢弃任务并抛出RejectedExecutionException异常；也是默认的处理方式。
            // ThreadPoolExecutor.DiscardPolicy：丢弃任务，但是不抛出异常。
            // ThreadPoolExecutor.DiscardOldestPolicy：丢弃队列最前面的任务，然后重新尝试执行任务（重复此过程）
            // ThreadPoolExecutor.CallerRunsPolicy：由调用线程处理该任务
            new ThreadPoolExecutor.DiscardOldestPolicy()
        );

        System.err.println("------2-----");
        Task task1 = new Task(1);
        tp.execute(task1);
        System.err.println(tp.getCorePoolSize()+"|"+tp.getPoolSize());
        System.err.println(tp.getQueue());
        System.err.println("-----1->2------");


        Task task2 = new Task(2);
        tp.execute(task2);
        System.out.println(tp.getCorePoolSize()+"|"+tp.getPoolSize());
        System.out.println(tp.getQueue());
        System.err.println("------2->3-----");


        Task task3 = new Task(3);
        tp.execute(task3);
        System.out.println(tp.getCorePoolSize()+"|"+tp.getPoolSize());
        System.out.println(tp.getQueue());
        System.err.println("------3->4-----");


        Task task4 = new Task(4);
        tp.execute(task4);
        System.out.println(tp.getCorePoolSize()+"|"+tp.getPoolSize());
        System.out.println(tp.getQueue());
        System.err.println("------4->5-----");


        Task task5 = new Task(5);
        tp.execute(task5);
        System.out.println(tp.getCorePoolSize()+"|"+tp.getPoolSize());
        System.out.println(tp.getQueue());
        System.err.println("------5->6-----");


        Task task7 = new Task(7);
        tp.execute(task7);
        System.out.println(tp.getCorePoolSize()+"|"+tp.getPoolSize());
        System.out.println(tp.getQueue());
        System.err.println("------7->8-----");

        Task task8 = new Task(8);
        tp.execute(task8);
        System.out.println(tp.getCorePoolSize()+"|"+tp.getPoolSize());
        System.out.println(tp.getQueue());
        System.err.println("------8->9-----");

        Task task9 = new Task(9);
        tp.execute(task9);
        System.out.println(tp.getCorePoolSize()+"|"+tp.getPoolSize());
        System.out.println(tp.getQueue());
        System.err.println("------9->10-----");

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        for(;;)
            endNum = Integer.parseInt(br.readLine().trim());
    }
    private static class Task implements Runnable{
        private int i;

        public Task(int i) {
            this.i = i;
        }

        @Override
        public void run() {
            System.err.println(Thread.currentThread().getName() + " Task " + i);
            while (endNum != i) {
                SleepHelper.sleepMill(100);
            }
            System.err.println(" Task " + i + "结束！");
        }

        @Override
        public String toString() {
            return "Task{" +
                "i=" + i +
                '}';
        }
    }

}
